package top.aicey.ai.sparring.processor;

import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RedisLogIndexContextProcessor implements LogIndexContextProcessor {

    @Autowired
    private RedissonClient redissonClient;

    private static final String LOG_INDEX_KEY = "LOG_INDEX_%s";

    @Override
    public Integer incr(String instanceId) {
        String remoteKey = String.format(LOG_INDEX_KEY,instanceId);

        long value = redissonClient.getAtomicLong(remoteKey).incrementAndGet();

        return Long.valueOf(value).intValue();
    }

    @Override
    public void clear(String instanceId) {
        String remoteKey = String.format(LOG_INDEX_KEY,instanceId);
        redissonClient.getAtomicLong(remoteKey).delete();
    }

}
